const sqlOptions = require("../../sql");
const site = require("express").Router()

// 保存
site.post("/save", async (req, res) => {
    try {
        const user_id = req.rows.id
        const {
            address_name,
            provinceId,
            cityId,
            districtId,
            mobile,
            remark,
            tag,
            isDefault
        } = req.body

        /*const list = await req.db.query(sqlOptions({
            name: "oms_site",
            method: "list",
            data: {
                user_id
            }
        }))*/

        if (isDefault === "0") {
            /*const sql = sqlOptions({
                name: "oms_site",
                method: "save",
                data: {
                    ...req.body,
                    user_id
                }
            })
            console.log(sql)*/
            await req.db.query(sqlOptions({
                name: "oms_site",
                method: "save",
                data: {
                    ...req.body,
                    user_id
                }
            }))

            return res.status(200).send({
                code: 200,
                message: "保存成功",
                data: ""
            })
        }
        if (isDefault === "1") {
            await req.db.query(sqlOptions({
                name: "oms_site",
                method: "update",
                data: {
                    isDefault: "0"
                },
                key: {
                    user_id
                }
            }))

            await req.db.query(sqlOptions({
                name: "oms_site",
                method: "save",
                data: {
                    ...req.body,
                    user_id
                }
            }))

            return res.status(200).send({
                code: 200,
                message: "保存成功",
                data: ""
            })
        }
    } catch (e) {
        return res.status(500).send({
            code: 500,
            message: "系统错误",
            data: ""
        })
    }
})

site.get("/list", async (req, res) => {
    try {
        const user_id = req.rows.id

        const list = await req.db.query(sqlOptions({
            name: "oms_site",
            method: "list",
            data: {
                user_id
            }
        }))

        const dist1 = await req.db.query(sqlOptions({
            name: "rc_district",
            method: "list",
            data: {
                level: '1'
            }
        }))

        const dist2 = await req.db.query(sqlOptions({
            name: "rc_district",
            method: "list",
            data: {
                level: '2'
            }
        }))

        const dist3 = await req.db.query(sqlOptions({
            name: "rc_district",
            method: "list",
            data: {
                level: '3'
            }
        }))
        if (list.length) {
            const siteList = list.map(item => {
                const dist1List = dist1.filter(i => i.district_id === item.provinceId)[0]
                const dist2List = dist2.filter(i => i.district_id === item.cityId)[0]
                const dist3List = dist3.filter(i => i.district_id === item. districtId)[0]

                const address = `${dist1List.district}-${dist2List.district}-${dist3List.district}-${item.remark}`
                return {
                    address,
                    tag: item.tag,
                    isDefault: item.isDefault,
                    address_name: item.address_name,
                    mobile: item.mobile,
                    id: item.id
                }
            })
            return res.status(200).send({
                code: 200,
                message: "",
                data: siteList
            })
        }
        return res.status(200).send({
            code: 200,
            message: "",
            data: []
        })
    } catch (e) {
        console.log(e)
        return res.status(500).send({
            code: 500,
            message: "系统错误",
            data: ""
        })
    }
})

site.post("/default", async (req, res) => {
    try {
        const user_id = req.rows.id
        const {id} = req.body

        await req.db.query(sqlOptions({
            name: "oms_site",
            method: "update",
            key: {
                user_id
            },
            data: {
                isDefault: "0"
            }
        }))

        await req.db.query(sqlOptions({
            name: "oms_site",
            method: "update",
            key: {
                id
            },
            data: {
                isDefault: "1"
            }
        }))

        return res.status(200).send({
            code: 200,
            message: "设置成功",
            data: ""
        })
    } catch (e) {
        return res.status(500).send({
            code: 500,
            message: "系统错误",
            data: ""
        })
    }
})

site.delete("/delete", async (req, res) => {
    try {
        const id = req.query.id
        await req.db.query(sqlOptions({
            method: "delete",
            name: "oms_site",
            data: {
                id
            }
        }))

        return res.status(200).send({
            code: 200,
            message: "删除成功",
            data: ""
        })
    } catch (e) {
        return res.status(500).send({
            code: 500,
            message: "系统错误",
            data: ""
        })
    }
})

module.exports = site